package com.hongxin.nw.netty.context;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.config.ApplicationContextProvider;
import com.hongxin.nw.netty.utils.MqSender;
import com.hongxin.nw.netty.utils.pools.*;
import com.hongxin.nw.netty.values.Catalogue;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StopState implements State {
 
   public void doAction(ChannelContext context) throws Exception {
      MqSender mqSender = ApplicationContextProvider.getBean(MqSender.class);
      String cid = context.getCtx().channel().id().asShortText();
      String code = CodePools.getCode(cid);
      CodePools.removeCode(cid);
      ChannelPools.removeCtx(code);
      HeartBeatPools.removeHB(cid);
      AsduStartPools.removeAsduStart(code);
      CodeTypePools.removeCodeType(code);

      JSONObject json = new JSONObject();
      json.put("machineId", code);
      json.put(Catalogue.HXCMD, Catalogue.OFFLINE);
      mqSender.send(Catalogue.STATUS,json);
      log.info("设备:{}, MSG:设备连接中断, 发送MQ{}", code, json);

      CodePools.printCtx();//打印所有在线设备code
   }
 
   public String toString(){
      return "Stop State";
   }
}